<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License.  You may obtain a copy of the License at
  ~
  ~   http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied.  See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
-->

<nz-spin [nzSpinning]="loading" [nzTip]="loadingTip" nzSize="large">
  <div class="-inner-content">
    <form nz-form #form="ngForm">
      <nz-form-item>
        <nz-form-label nzSpan="7" [nzRequired]="true" [nzFor]="'host'">{{ hostName ? hostName : ('monitor.host' | i18n) }} </nz-form-label>
        <nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
          <app-form-field
            [item]="{
              field: 'host',
              type: 'text',
              required: true,
              placeholder: 'monitor.host.tip' | i18n
            }"
            [name]="'host'"
            [(ngModel)]="monitor.host"
            (ngModelChange)="onHostChange($event)"
          />
        </nz-form-control>
      </nz-form-item>

      <nz-form-item>
        <nz-form-label nzSpan="7" [nzRequired]="true" [nzFor]="'name'">{{ 'monitor.name' | i18n }} </nz-form-label>
        <nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
          <app-form-field
            [item]="{
              field: 'name',
              type: 'text',
              required: true,
              placeholder: 'monitor.name.tip' | i18n
            }"
            [name]="'name'"
            [(ngModel)]="monitor.name"
          />
        </nz-form-control>
      </nz-form-item>

      <ng-container *ngFor="let paramDefine of paramDefines; let i = index">
        <nz-form-item *ngIf="params[i].display !== false && paramDefine.field !== 'host'">
          <nz-form-label nzSpan="7" [nzRequired]="paramDefine.required" [nzFor]="paramDefine.field">{{ paramDefine.name }} </nz-form-label>
          <nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
            <app-form-field
              [item]="paramDefine"
              [name]="paramDefine.field"
              [(ngModel)]="params[i].paramValue"
              (ngModelChange)="
                paramDefine.type === 'boolean'
                  ? onParamBooleanChanged($event, paramDefine.field)
                  : paramDefine.type === 'radio'
                  ? onDependChanged($event, paramDefine.field)
                  : null
              "
            />
          </nz-form-control>
        </nz-form-item>
      </ng-container>

      <nz-collapse *ngIf="hasAdvancedParams" [nzGhost]="true" style="background: ghostwhite; margin-bottom: 24px">
        <ng-template #extraColHeader>
          <button style="margin-left: 40%" nz-button nzType="dashed" nz-tooltip [nzTooltipTitle]="'monitors.advanced.tip' | i18n">
            <span>{{ 'monitors.advanced' | i18n }}</span>
            <i nz-icon nzType="down-circle" nzTheme="outline"></i>
          </button>
        </ng-template>
        <nz-collapse-panel [nzHeader]="extraColHeader" [nzShowArrow]="false">
          <ng-container *ngFor="let paramDefine of advancedParamDefines; let i = index">
            <nz-form-item *ngIf="advancedParams[i].display !== false && paramDefine.field !== 'host'">
              <nz-form-label nzSpan="7" [nzRequired]="paramDefine.required" [nzFor]="paramDefine.field"
                >{{ paramDefine.name }}
              </nz-form-label>
              <nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
                <app-form-field [item]="paramDefine" [name]="paramDefine.field" [(ngModel)]="advancedParams[i].paramValue" />
              </nz-form-control>
            </nz-form-item>
          </ng-container>
        </nz-collapse-panel>
      </nz-collapse>

      <nz-form-item>
        <nz-form-label nzSpan="7" [nzFor]="'collector'" [nzTooltipTitle]="'monitor.collector.tip' | i18n"
          >{{ 'monitor.collector' | i18n }}
        </nz-form-label>
        <nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
          <app-form-field
            [item]="{
              field: 'collector',
              type: 'collectors-selection'
            }"
            [extra]="{collectors}"
            [name]="'collector'"
            [(ngModel)]="collector"
          />
        </nz-form-control>
      </nz-form-item>

      <nz-form-item>
        <nz-form-label nzSpan="7" [nzFor]="'intervals'" [nzTooltipTitle]="'monitor.intervals.tip' | i18n"
          >{{ 'monitor.intervals' | i18n }}
        </nz-form-label>
        <nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
          <app-form-field
            [item]="{
              field: 'intervals',
              type: 'intervals'
            }"
            [extra]="{ interval_type: monitor.app }"
            [name]="'intervals'"
            [(ngModel)]="monitor.intervals"
          />
        </nz-form-control>
      </nz-form-item>

      <nz-form-item>
        <nz-form-label nzSpan="7" [nzFor]="'tags'" [nzTooltipTitle]="'tag.bind.tip' | i18n">{{ 'tag.bind' | i18n }} </nz-form-label>
        <nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
          <app-form-field
            [item]="{
              field: 'tags',
              type: 'tags-selection'
            }"
            [name]="'tags'"
            [(ngModel)]="monitor.tags"
          />
        </nz-form-control>
      </nz-form-item>

      <nz-form-item>
        <nz-form-label nzSpan="7" [nzFor]="'description'" [nzTooltipTitle]="'monitor.description.tip' | i18n"
          >{{ 'monitor.description' | i18n }}
        </nz-form-label>
        <nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
          <app-form-field
            [item]="{
              field: 'description',
              type: 'textarea-limit'
            }"
            [name]="'description'"
            [(ngModel)]="monitor.description"
          />
        </nz-form-control>
      </nz-form-item>

      <nz-divider *ngIf="monitor.app === 'prometheus'"></nz-divider>
      <!-- grafana is enabled -->
      <nz-form-item *ngIf="monitor.app === 'prometheus'">
        <nz-form-label nzSpan="7" nzFor="grafana" [nzTooltipTitle]="'monitor.grafana.enabled.tip' | i18n">
          {{ 'monitor.grafana.enabled.label' | i18n }}
        </nz-form-label>
        <nz-form-control nzSpan="8">
          <nz-switch [(ngModel)]="grafanaDashboard.enabled" name="grafana" id="grafana"></nz-switch>
        </nz-form-control>
      </nz-form-item>
      <!-- upload grafana dashboard json -->
      <nz-form-item *ngIf="monitor.app === 'prometheus' && grafanaDashboard.enabled">
        <nz-form-label nzSpan="7" nzFor="grafanaJson" [nzTooltipTitle]="'monitor.grafana.upload.tip' | i18n">
          {{ 'monitor.grafana.upload.label' | i18n }}
        </nz-form-label>
        <nz-form-control nzSpan="8">
          <nz-upload
            nzName="file"
            nzListType="text"
            [nzShowUploadList]="false"
            [nzMultiple]="false"
            [nzLimit]="1"
            [nzCustomRequest]="handleTemplateInput"
            (nzChange)="handleTemplateInput($event)"
          >
            <button nz-button>
              <i nz-icon nzType="upload"></i>
              {{ 'monitor.grafana.upload.label' | i18n }}
            </button>
          </nz-upload>
        </nz-form-control>
      </nz-form-item>

      <div nz-row>
        <div nz-col [nzXs]="{ span: 24 }" [nzLg]="{ span: 8, offset: 7 }" style="text-align: center">
          <button
            nz-button
            nzType="primary"
            type="submit"
            nz-tooltip
            [nzTooltipTitle]="'monitors.detect.tip' | i18n"
            (click)="onDetect(form.form)"
          >
            {{ 'common.button.detect' | i18n }}
          </button>
          <button nz-button nzType="primary" type="submit" (click)="onSubmit(form.form)"> {{ 'common.button.ok' | i18n }} </button>
          <button nz-button nzType="primary" type="reset" (click)="onCancel()"> {{ 'common.button.cancel' | i18n }} </button>
        </div>
      </div>
    </form>
  </div>
</nz-spin>
